from fastapi import Depends
from fastapi import FastAPI, UploadFile, File
from pydantic import BaseModel, Field, model_validator

app = FastAPI()


class User(BaseModel):
    username: str = Field(..., title='姓名', description='姓名字段需要长度大于6且小于等于12', max_length=12,
                          min_length=6, example="Foo")
    age: int = Field(..., title='年龄', description='年龄需要大于18岁', ge=18, example=12)
    password_old: str = Field(..., title='旧密码', description='密码需要长度大于6', gl=6, example=6)
    password_new: str = Field(..., title='新密码', description='密码需要长度大于6', gl=6, example=6)

    @model_validator(mode="after")
    def check_passwords(self):
        password_old, password_new = self.password_old, self.password_new
        # 新旧号码的确认匹配处理
        if password_old and password_new and password_old != password_new:
            raise ValueError('passwords do not match')
        return self


@app.get("/user")
def read_user(user: User = Depends()):
    return {
        'username': user.username,
        'password_old': user.password_old,
        'password_new': user.password_new,
    }


class FileGet(BaseModel):
    username: str = Field(..., title='姓名', description='姓名字段需要长度大于6且小于等于12', max_length=12,
                          min_length=6, example="Foo")
    file: UploadFile = File(...)


@app.post("/file_get")
async def file_get(user: FileGet = Depends()):
    return {
        'username': user.username,
        'filenme': user.file.filename
    }


if __name__ == "__main__":
    import uvicorn
    import os

    app_model_name = os.path.basename(__file__).replace(".py", "")
    print(app_model_name)
    uvicorn.run(f"{app_model_name}:app", host='127.0.0.1')
